#include <stdint.h>
#include "includes.h"


enum
{
    RESET_CMD=0x08,
    CTRL_CMD=0xA0,
    STATE_QUERY_CMD=0x04,
    UPGRADE_CMD=0x03
}COMM_CMD_t;

typedef struct
{
    //帧头
    uint8_t frame_header_h;
    uint8_t frame_header_l;
    //帧长
    uint8_t frame_len_h;
    uint8_t frame_len_l;
    //帧类别
    uint8_t frame_type;
    //数据
    uint8_t data[6];
    //crc
    uint8_t crc_h;
    uint8_t crc_l;
}rs485_frame_t;

uint16_t CRC16_MODBUS(unsigned char *buffer, uint16_t length)
{
    unsigned char i;
    uint16_t pos;

    uint16_t Crc_16;

    Crc_16 = 0xFFFF; // 初始值
    for ( pos = 0; pos < length; pos++) {
        Crc_16 ^= buffer[pos];
        for ( i = 8; i != 0; i--) {  // 处理每一位
            if ((Crc_16 & 0x0001) != 0) {
                Crc_16 >>= 1;
                Crc_16 ^= 0xA001;
            } else {
                Crc_16 >>= 1;
            }
        }
    }
	return  Crc_16;
}


// uint16_t rs485_tx_poll_time_count = 0;
static void rs485_tx_poll(void)
{
    static u8 state_machine = 0;

    switch(state_machine)
    {
        case 0:
        if(true == Com0.Tx_Complete)
        {
            if(RESET != usart_flag_get(USART0, USART_FLAG_TC))
            {
                state_machine = 1;
            }
            // rs485_tx_poll_time_count=0;
        }
        break;

        case 1:
        Com0.Tx_Complete = false;
        set_rs485_direction(RX);
        state_machine = 0;
        break;

        default:
        break;
    }
}

void rs485_poll(void)
{
    rs485_tx_poll();

    
}


